package com.zhixianggou.caibaobox.user.shiro.utils;


import com.zhixianggou.caibaobox.user.shiro.ShiroUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.subject.support.WebDelegatingSubject;
import org.springframework.stereotype.Component;


@Component
public class ShiroUtil {
	
	public static void setCurrentUser(ShiroUser user) {
		Subject subject = SecurityUtils.getSubject();
		subject.getSession().setAttribute("userInfo", user);
	}

	
	
	public static ShiroUser getCurrentUser() {
		Subject user = SecurityUtils.getSubject();

		ShiroUser ur = null;
		if (user instanceof WebDelegatingSubject) {
			ur = (ShiroUser) user.getPrincipal();
		}
		return ur;
	}
	
	
	
	public static Integer getCurrentUserId() {
		ShiroUser user = getCurrentUser();
		if (null != user ) {
			return user.getUserId();
		}
		
		return 0;
	}
}
